package com.quasar.hpatient.module.home_urinevolume;

import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.CalendarDialog;
import com.quasar.hpatient.dialog.InputDialog;
import com.quasar.hpatient.dialog.TimeDialog;
import com.quasar.hpatient.dialog.WarningDialog;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.chart.ChartView;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 首页 ==> 出入量
 * created by kalu on 2018/4/25 9:23
 */
public class UrinevolumeActivity extends BaseActivity<UrinevolumePresenter> implements UrinevolumeView {

    public static final String MULIT = "MULIT";
    public static final String DATE = "DATE";

    @Override
    public int initView() {
        return R.layout.activity_home_urinevolume;
    }

    @Override
    public void initDataLocal() {

        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);

        final NavigationView menu = findViewById(R.id.home_urinevolume_menu);
        menu.setMenuEnable(!mulit);
        if (mulit) {
            menu.setOnBackChangeListener(() -> onBackPressed());
        } else {
            menu.setOnNavigationChangeListener((isBack, isMenu) -> {
                if (isBack) onBackPressed();
                else if (isMenu) {
                    final RecyclerView recycler = findViewById(R.id.activity_home_urinevolume_list);
                    final ChartView chart = findViewById(R.id.activity_home_urinevolume_chart);
                    final EditText edit = findViewById(R.id.activity_home_urinevolume_input);
                    getPresenter().saveUrinevolume(this, chart, recycler, edit);
                }
            });
        }

        final RecyclerView recycler = findViewById(R.id.activity_home_urinevolume_list);
        getPresenter().initList(this, recycler, mulit);

        findViewById(R.id.activity_home_urinevolume_date).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_urinevolume_group).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_urinevolume_menu2).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_urinevolume_menu4).setVisibility(mulit ? View.GONE : View.VISIBLE);

        if (!mulit) {
            findViewById(R.id.activity_home_urinevolume_input).requestFocus();
            findViewById(R.id.activity_home_urinevolume_time).setOnClickListener(v -> showTime());
            final TextView dates = findViewById(R.id.activity_home_urinevolume_date);
            final TextView times = findViewById(R.id.activity_home_urinevolume_time);
            final String stringExtra = getIntent().getStringExtra(DATE);

            if (TextUtils.isEmpty(stringExtra)) {
                getPresenter().initTime(this, dates, times);
            } else {
                setText(dates, stringExtra);
                getPresenter().initTime(this, null, times);
            }

            final TextView date = findViewById(R.id.activity_home_urinevolume_date);
            date.setOnClickListener(v -> {

                final String trim = date.getText().toString().trim();
                try {
                    final int day = Integer.parseInt(trim.substring(trim.length() - 2, trim.length()));
                    showCalendar(date, day);
                } catch (Exception e) {
                    LogUtil.e("", e.getMessage(), e);
                }
            });
        } else {
            TextView date = findViewById(R.id.activity_home_urinevolume_date);
            final String stringExtra = getIntent().getStringExtra(DATE);
            setText(date, stringExtra);
        }

        TextView hint = findViewById(R.id.home_urinevolume_hint);
        RadioGroup group = findViewById(R.id.home_urinevolume_group);
        group.setOnCheckedChangeListener((group1, checkedId) -> {
            if (checkedId == R.id.home_urinevolume_button1) {
                hint.setText("入量");
            } else if (checkedId == R.id.home_urinevolume_button2) {
                hint.setText("白天尿量");
            } else {
                hint.setText("夜尿量");
            }
        });
    }

    @Override
    public void initDataNet() {
        final TextView date = findViewById(R.id.activity_home_urinevolume_date);
        final RecyclerView recycler = findViewById(R.id.activity_home_urinevolume_list);
        final ChartView chart = findViewById(R.id.activity_home_urinevolume_chart);
        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
        getPresenter().urinevolumeList(this, recycler, chart, date, mulit);
    }

    @Override
    public void showDelete(String str, int position) {

        WarningDialog dialog = new WarningDialog(this);
        dialog.show();
        dialog.setTitles("确定删除" + str);
        dialog.setButton("取消", "确定");
        dialog.setOnDialogChangeListener((left, right) -> {
            if (right) {
                final ChartView chart = findViewById(R.id.activity_home_urinevolume_chart);
                final RecyclerView recycler = findViewById(R.id.activity_home_urinevolume_list);
                getPresenter().deleteUrinevolume(UrinevolumeActivity.this, recycler, chart, position);
            }
        });
    }

    @Override
    public void showTime() {

        final TimeDialog dialog = new TimeDialog(this);
        dialog.show();
        dialog.setTitles("选择时间");
        dialog.setOnTimeChangeListener((hour, minute) -> {
            final TextView time1 = findViewById(R.id.activity_home_urinevolume_time);
            setText(time1, hour + ":" + minute);
        });
    }

    @Override
    public void showInput(String value, String name, int position) {

        InputDialog dialog = new InputDialog(this);
        dialog.show();
        dialog.setTities("修改" + name);
        dialog.setMax(5000);
        dialog.setMin(0);
        dialog.setDotLength(0);
        dialog.setMaxLength(4);
        dialog.setMenu(name + ":");
        dialog.setInputType(InputType.TYPE_CLASS_NUMBER);
        dialog.setHint("请输入数值");
        dialog.setInput(value);
        dialog.setOnDialogChangeListener(input -> {
            final ChartView chart = findViewById(R.id.activity_home_urinevolume_chart);
            final RecyclerView recycler = findViewById(R.id.activity_home_urinevolume_list);
            getPresenter().modifyUrinevolume(UrinevolumeActivity.this, recycler, chart, input, position);
        });
    }

    @Override
    public void showCalendar(TextView text, int day) {

        final CalendarDialog dialog = new CalendarDialog(this);
        dialog.setScroll(getDate());
        dialog.show();
        dialog.setOnCalendarChangeListener((years, months, days, maxDay, isClick) -> {
            if (isClick) {
                final TextView date = findViewById(R.id.activity_home_urinevolume_date);
                setText(date, years + "-" + months + "-" + days);
                final RecyclerView recycler = findViewById(R.id.activity_home_urinevolume_list);
                final ChartView chart = findViewById(R.id.activity_home_urinevolume_chart);
                final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
                TextView date1 = findViewById(R.id.activity_home_urinevolume_date);
                getPresenter().urinevolumeList(UrinevolumeActivity.this, recycler, chart, date1, mulit);
            } else {
                getPresenter().schemelList(dialog, years, months, days, maxDay);
            }
        });
    }

    @Override
    public void setChartNull() {
        final ChartView chart = findViewById(R.id.activity_home_urinevolume_chart);
        if (null == chart) return;
        chart.setHintText(getResources().getString(R.string.chart_null));
    }

    @Override
    public String getDate() {
        final TextView text = findViewById(R.id.activity_home_urinevolume_date);
        return text.getText().toString();
    }

    @Override
    public String getTime() {
        final TextView text = findViewById(R.id.activity_home_urinevolume_time);
        return text.getText().toString();
    }

    @Override
    public String getName() {
        final RadioGroup group = findViewById(R.id.home_urinevolume_group);
        final TextView text = findViewById(group.getCheckedRadioButtonId());
        return text.getText().toString();
    }

    @Override
    public String getUrinevolume() {
        final TextView text = findViewById(R.id.activity_home_urinevolume_input);
        return text.getText().toString();
    }
}